/**
 * 
 */
package org.coldlife.sim.grav;

public class SimulationTimer implements Runnable {
	private long lastFrame = System.nanoTime();
	private boolean isDone = false;
	private World world;
	
	public SimulationTimer(World world) 
	{
		this.setWorld(world);
	}
	
	public void setDone(boolean done)
	{
		isDone = done;
	}
	
	public boolean isDone()
	{
		return isDone;
	}
	
	public long getLastFrame() 
	{
		return lastFrame;
	}

	public void setLastFrame(long lastFrame) 
	{
		this.lastFrame = lastFrame;
	}

	private void setWorld(World world) {
		this.world = world;
	}

	private World getWorld() {
		return world;
	}

	public void run() 
	{
		while(!isDone())
		{
			long time = System.nanoTime();
			long dt = time - getLastFrame();
			setLastFrame(time);
			
			getWorld().update(dt*1e-9);// convert from nanoseconds
			
			// play nice and share CPU time
			try 
			{
				Thread.sleep(7);
			} 
			catch (InterruptedException e) 
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}